package com.ssm.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.ssm.model.Contacts;
import com.ssm.model.Customer;
import com.ssm.model.Project;
import com.ssm.service.ContactsService;
import com.ssm.service.CustomerService;
import com.ssm.util.TimeUtil;

@Controller
public class ContactsController {

	@Autowired
	public ContactsService contactsService;

	@Autowired
	public CustomerService customerService;

	
	/**
	 * test 分页
	 * @param contacts 联系人
	 * @param mp
	 * @return jsonArray.fromObject(lists).toString()
	 */
	@RequestMapping(value = "/contacts", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	@ResponseBody
	public String test(HttpServletRequest request,
			HttpServletResponse response, Contacts contacts, ModelMap mp) {
		
			//当前页
			int ye = Integer.valueOf(request.getParameter("userId"));  
			//当前页的第一条数据
			int shou = ye * 5 - 5;
			//每页的数据条数
			int wei = 5;
			//获取客户名
			String customerName = request.getParameter("customer");
		
			System.out.println(customerName);
			//获取对应客户名的数据
			List<Customer> customer = customerService
					.findCustomerByName(customerName);
			//获取对应客户Id的联系人，并进行分页
			List<Contacts> lists = contactsService.findAllContacts2(shou, wei,
					customer.get(0).getCustomerId());
			//返回Json
			JSONArray jsonArray = new JSONArray();
			jsonArray.fromObject(lists);
			return jsonArray.fromObject(lists).toString();
		
		
	}

	
	/**
	 * contactspage 计算总记录数
	 * @return	jsonObject.toString();
	 */
	@RequestMapping(value = "contactspage", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	@ResponseBody
	public String contactspage(HttpServletRequest request,
			HttpServletResponse response, Contacts contacts, ModelMap mp) {
		String customerName = request.getParameter("customer");
		System.out.println(customerName);
		List<Customer> customer = customerService
				.findCustomerByName(customerName);
		int id = customer.get(0).getCustomerId();
		List<Contacts> lists = new ArrayList<Contacts>();
		//通过客户Id(CustomerId)查找对应的Contacts数据
		lists = contactsService.findAllContactsByCustomerId(id);
		System.out.println(lists.size());
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("num", lists.size());
		return jsonObject.toString();
	}

	// @RequestMapping(value = "/contacts1", method = RequestMethod.POST,
	// produces = "text/plain;charset=UTF-8")
	// @ResponseBody
	// public String test111(HttpServletRequest request,
	// HttpServletResponse response, Contacts contacts, ModelMap mp) {
	// System.out.println(contacts);
	// List<Contacts> lists = new ArrayList<Contacts>();
	// JSONArray jsonArray = new JSONArray();
	// lists = contactsService.findAllContacts();
	// return jsonArray.fromObject(lists).toString();
	// }

	/**
	 * test1  增加
	 */
	@RequestMapping(value = "/contacts_zj", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	@ResponseBody
	public String test1(HttpServletRequest request,
			HttpServletResponse response, Contacts contacts, ModelMap mp) {
		 String contactsName = request.getParameter("contactsName");
		 String contactsPhone = request.getParameter("contactsPhone");
		// 获取当前时间
		String createTime = TimeUtil.timeToString(TimeUtil.currentTime());
		// System.out.println("===================================");
		// Contacts contacts2 = new Contacts(contactsName, contactsPhone,
		// createTime);
		String customerName = request.getParameter("customet");
		System.out.println("-------" + customerName);
		
			int customerId = customerService.findCustomerByName(customerName).get(0)
					.getCustomerId();
			System.out.println(customerId);
			//把获取到的customer传入到contacts中
			contacts.setCustomerId(customerId);
			contacts.setCreateTime(createTime);
			//把contacts添加到数据库中
			contactsService.contacts_zj(contacts);
			List<Contacts> lists = new ArrayList<Contacts>();
			JSONArray jsonArray = new JSONArray();
			return jsonArray.fromObject(lists).toString();
		
		
	}

	
	/**
	 * test2 修改
	 */
	@RequestMapping(value = "/contacts_xg", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	@ResponseBody
	public String test2(HttpServletRequest request,
			HttpServletResponse response, Contacts contacts, ModelMap mp) {
		// int id=Integer.parseInt(request.getParameter("contactsId"));
		// contacts=contactsService.findcontactsById(id);
		// System.out.println(id);
		// String contactsName=request.getParameter("contactsName");
		// String contactsPhone=request.getParameter("contactsPhone");
		// String createTime=request.getParameter("createTime");
		// System.out.println("=========================");
		// contacts.setContactsName(contactsName);
		// contacts.setContactsPhone(contactsPhone);
		// contacts.setCreateTime(createTime);
		
		//修改内容后传给数据库
		contactsService.contacts_xg(contacts);
		List<Contacts> lists = new ArrayList<Contacts>();
		JSONArray jsonArray = new JSONArray();
		return jsonArray.fromObject(lists).toString();
	}

	/**
	 * test3 删除
	 */
	@RequestMapping(value = "/contacts_sc", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	@ResponseBody
	public String test3(HttpServletRequest request,
			HttpServletResponse response, Contacts contacts, ModelMap mp) {
		int id = Integer.parseInt(request.getParameter("contactsId"));
		//通过Id删除contacts
		contactsService.contacts_sc(id);
		List<Contacts> lists = new ArrayList<Contacts>();
		JSONArray jsonArray = new JSONArray();
		return jsonArray.fromObject(lists).toString();
	}
}
